package com.hns.can.service.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.service.DriverExamService;
import com.hns.can.service.service.SafeBehaviourService;
import com.hns.can.service.vo.DriverCommentParamsVo;
import com.hns.can.service.vo.SafeBehaviourVo;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 安全行为controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/safeBehaviour")
public class SafeBehaviourController extends BaseController {

    @Autowired
    private DriverExamService driverExamService;
    @Autowired
    private SafeBehaviourService safeBehaviourService;

    final private String[] headerNames = {"排名", "得分", "司机", "营运线路", "所属机构", "运行时长", "运行里程(KM)", "行为总数(次)", "异常开门(次)", "异常关门(次)", "空挡滑行(次)", "倒车加速(次)", "ABS(次)", "夜间行车(次)", "起步急加(次)", "熄火滑行(次)", "急加速(次)", "急减速(次)", "急刹(次)", "超速(次)", "行为次数(次/千公里)"};
    final private String[] headerFields = {"allRank", "distanScore", "drvName", "lineName", "organName", "lastingTime", "totalMile", "bhvAll", "summary1", "summary2", "summary5", "summary6", "summary7", "summary8", "summary9", "summary10", "summary11", "summary12", "summary13", "overSpdingCount", "bhvPerMile"};

    /**
     * 分页查询安全行为
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchSafeBehaviour")
    @ResponseBody
    public Object searchSafeBehaviour(CommonParamsVo vo) throws Exception {
        //由于和节能查询条件相同，条件参数vo用节能条件参数vo
        SysUserEntity user = this.getCurrentUser().getUser();
        driverExamService.initParams(vo, user, "T_COMM_DRV_SCT_BHV_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        Pagination page = safeBehaviourService.searchSafeBehaviour(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 获取安全行为评语
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getSafeBehaviourComments")
    @ResponseBody
    public Object getSafeBehaviourComments(DriverCommentParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        //数组：{评语，评价级别}
        List<String> list = driverExamService.getComments(vo, user, new String[]{"getCommentContent4Bhv", "getCommentGrade4Bhv"});
        return this.getReturnResult(list);
    }

    /**
     * 获取安全行为用于导出
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    private List<SafeBehaviourVo> getSafeBehaviourFileData(CommonParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        driverExamService.initParams(vo, user, "T_COMM_DRV_SCT_BHV_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        List<SafeBehaviourVo> list = safeBehaviourService.getAllSafeBehaviour(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 导出安全行为Excel文档
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportSafeBehaviourExcel")
    public void exportSafeBehaviourExcel(CommonParamsVo vo, HttpServletResponse response) throws Exception {
        List<SafeBehaviourVo> list = getSafeBehaviourFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", URLEncoder.encode("安全行为评分", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "安全行为");
    }

    /**
     * 导出安全行为Pdf文档
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportSafeBehaviourPdf")
    public void exportSafeBehaviourPdf(CommonParamsVo vo, HttpServletResponse response) throws Exception {
        List<SafeBehaviourVo> list = getSafeBehaviourFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("安全行为评分", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "安全行为");
    }
}
